package com.leha.forum.lehare.mapper;

import com.leha.forum.lehare.entity.Level;
import org.apache.ibatis.annotations.*;

import java.util.List;
@Mapper
public interface LevelDAO {
    //1. 新增等级(管理员设置等级规则)
    @Insert("INSERT INTO level(level_name, post_count, reply_count, essence_count, " +
            "min_experience, icon_url, title) " +
            "VALUES(#{levelName}, #{postCount}, #{replyCount}, #{essenceCount}, " +
            "#{minExperience}, #{iconUrl}, #{title})")
    @Options(useGeneratedKeys = true, keyProperty = "levelId")
    void insert(Level level);


    //2. 根据levelid查询等级信息
    @Select("SELECT * FROM level WHERE level_id = #{levelId}")
    Level selectLevelById(int levelId);

    //3. 根据最小经验值查询该等级的所有信息
    @Select("SELECT * FROM level ORDER BY min_experience ASC")
    List<Level> selectAll();

    //4. 根据levelId修改信息(修改该等级下的信息)
    @Update("UPDATE level SET level_name=#{levelName}, post_count=#{postCount}, " +
            "reply_count=#{replyCount}, essence_count=#{essenceCount}, " +
            "min_experience=#{minExperience}, icon_url=#{iconUrl}, title=#{title} " +
            "WHERE level_id=#{levelId}")
    void update(Level level);

    //5. 根据levelId删除等级
    @Delete("DELETE FROM level WHERE level_id = #{levelId}")
    void deleteLevelById(Integer levelId);

    //6. 通过经验值查询等级信息
    @Select("SELECT * FROM level WHERE min_experience <= #{experience} ORDER BY min_experience DESC")
    List<Level> selectByExperience(Integer experience);
    // 在 LevelMapper.java 中定义
    @Select("SELECT * FROM level WHERE min_experience > #{experience} " +
            "ORDER BY min_experience ASC LIMIT 1")
    Level selectNextLevel(Integer experience);
}
